﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Movies.Web.Models.ViewModel
{
    public class MovieViewModel
    {
        public MovieViewModel()
        {
            ActorIds = new List<int>();
        }

        [Required()]
        public string MovieName { get; set; }
       

        [Required(), DataType(DataType.Date)]
        public DateTime? ReleaseDate { get; set; }

        [Required()]
        [UIHint("Rating")]
        public string Rating { get; set; }

        [UIHint("actors")]
        public IList<int> ActorIds { get; set; }

        [UIHint("Director")]
        public int? SelectedDirector { get;set;}

        public Movie MapToMovie(IList<Actor> actors, Director directorOfMovie, int Id)
        {
            var movieResult = new Movie();

            movieResult.Name = this.MovieName;
            movieResult.Rating = this.Rating;
            movieResult.RelaseDate = this.ReleaseDate;
            movieResult.Id = Id;
            foreach (var actor in actors)
            {
                actor.MoviesStaredIn.Add(movieResult);
                movieResult.Actors.Add(actor);

            }

            movieResult.DirectedBy = directorOfMovie;
            movieResult.DirectedBy.MoviesDirected.Add(movieResult);
            return movieResult;

        }

        public MovieViewModel MapToModel(Movie movie)
        {
            this.MovieName = movie.Name;
            this.Rating = movie.Rating;
            this.ReleaseDate = movie.RelaseDate;
            this.ActorIds = movie.Actors.Select(x => x.Id).ToArray();
            this.SelectedDirector = movie.DirectedBy.Id;
            return this;
        }

    }
}